package com.momo.service.sysSetting;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.momo.common.common.DateUtils;
import com.momo.common.common.JSONResult;
import com.momo.common.req.sysSetting.group.GroupRoleReq;
import com.momo.common.req.sysSetting.group.UserGroupReq;
import com.momo.common.req.sysSetting.group.VipUserReq;
import com.momo.common.req.sysSetting.user.SysUserReq;
import com.momo.common.req.sysmain.RedisUser;
import com.momo.common.util.DateUtil;
import com.momo.common.util.Encrypt;
import com.momo.common.util.JwtTokenUtil;
import com.momo.common.util.StrUtil;
import com.momo.mapper.dataobject.RoleDO;
import com.momo.mapper.dataobject.UserDO;
import com.momo.mapper.dataobject.UserGroupDO;
import com.momo.mapper.mapper.sysSetting.RoleMapper;
import com.momo.mapper.mapper.sysSetting.UserMapper;
import com.momo.mapper.mapper.sysSetting.VipMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * Created by MOMO on 2019/4/16.
 */
@Service
@Slf4j
public class VipServiceImpl implements VipService {
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public JSONResult vipPage(UserGroupReq vipReq) {
        UserGroupDO userGroupDO = new UserGroupDO();
        BeanUtils.copyProperties(vipReq, userGroupDO);
        PageHelper.startPage(vipReq.getPageNum(), vipReq.getPageSize(), "id desc");
        List<UserGroupDO> list = vipMapper.vipPage(userGroupDO);
        PageInfo<UserGroupDO> pageInfo = new PageInfo<>(list);
        return JSONResult.ok(pageInfo);
    }

    @Override
    public JSONResult vipUserPage(VipUserReq vipReq) {
        UserGroupDO userGroupDO = vipMapper.uuid(vipReq.getUserGroupUuid());
        if (null == userGroupDO) {
            return JSONResult.errorMap("待查询的组织不存在");
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(vipReq, userDO);
        userDO.setGroupId(userGroupDO.getId());
        PageHelper.startPage(vipReq.getPageNum(), vipReq.getPageSize(), "id desc");
        List<UserDO> list = userMapper.userPage(userDO);
        PageInfo<UserDO> pageInfo = new PageInfo<>(list);
        return JSONResult.ok(pageInfo);
    }

    @Override
    public JSONResult save(UserGroupReq vipReq, RedisUser redisUser) {
        int vipNameCount = vipMapper.checkVipName(vipReq.getUserGroupName(), null);
        if (vipNameCount > 0) {
            return JSONResult.errorMap("企业/VIP 已存在");
        }
        UserGroupDO userGroupDO = new UserGroupDO();
        BeanUtils.copyProperties(vipReq, userGroupDO);
        userGroupDO.setCreateBy(redisUser.getSysUserName());
        userGroupDO.setUpdateBy(redisUser.getSysUserName());
        userGroupDO.setCreateTime(DateUtil.getDateTime());
        userGroupDO.setUpdateTime(DateUtil.getDateTime());
        userGroupDO.setUserGroupUuid(StrUtil.genUUID());
        userGroupDO.setSysAccountStartTime(new Date());
        // todo 根据自己规则定制  后期更改
        userGroupDO.setSysAccountEndTime(DateUtils.localDateTimeTOdate(DateUtils.addTime(new Date(), ChronoUnit.YEARS,1)));
        vipMapper.insertSelective(userGroupDO);
        return JSONResult.ok("新增企业/VIP成功");
    }

    @Override
    public JSONResult userAdd(SysUserReq vipReq, RedisUser redisUser) {
        UserGroupDO userGroupDO = vipMapper.uuid(vipReq.getUserGroupUuid());
        if (null == userGroupDO) {
            return JSONResult.errorMap("待查询的组织不存在");
        }
        if (checkInfo(vipReq.getSysUserEmail(), null, null, null)) {
            return JSONResult.errorMap("邮箱已被使用");
        }
        if (checkInfo(null, vipReq.getSysUserPhone(), null, null)) {
            return JSONResult.errorMap("手机号已被使用");
        }
        if (checkInfo(null, null, vipReq.getSysUserLoginName(), null)) {
            return JSONResult.errorMap("登录账号已被使用");
        }
        final String randomKey = jwtTokenUtil.getRandomKey();
        String pwd = Encrypt.getShiroPassword(vipReq.getSysUserPwd(), randomKey, 2);
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(vipReq, userDO);
        userDO.setGroupId(userGroupDO.getId());
        userDO.setSysUserPwd(pwd);
        userDO.setSysUserAuthSalt(randomKey);
        userDO.setGroupId(userGroupDO.getId());
        userDO.setCreateBy(redisUser.getSysUserName());
        userDO.setUpdateBy(redisUser.getSysUserName());
        userDO.setCreateTime(DateUtil.getDateTime());
        userDO.setUpdateTime(DateUtil.getDateTime());
        userDO.setSysUserUuid(StrUtil.genUUID());
        userMapper.insertSelective(userDO);
        return JSONResult.ok("新增用户成功");
    }

    @Override
    public JSONResult uuid(UserGroupReq vipReq) {
        UserGroupDO userGroupExists = vipMapper.uuid(vipReq.getUserGroupUuid());
        if (null == userGroupExists) {
            return JSONResult.errorMap("待查询的企业/VIP不存在");
        }
        return JSONResult.ok(userGroupExists);
    }

    @Override
    public JSONResult modify(UserGroupReq vipReq, RedisUser redisUser) {
        UserGroupDO userGroupExists = vipMapper.uuid(vipReq.getUserGroupUuid());
        if (null == userGroupExists) {
            return JSONResult.errorMap("待查询的企业/VIP不存在");
        }
        int vipNameCount = vipMapper.checkVipName(vipReq.getUserGroupName(), userGroupExists.getId());
        if (vipNameCount > 0) {
            return JSONResult.errorMap("企业/VIP 已存在");
        }
        UserGroupDO userGroupDO = new UserGroupDO();
        BeanUtils.copyProperties(vipReq, userGroupDO);
        userGroupDO.setId(userGroupExists.getId());
        userGroupDO.setUpdateBy(redisUser.getSysUserName());
        userGroupDO.setUpdateTime(DateUtil.getDateTime());
        vipMapper.updateByPrimaryKeySelective(userGroupDO);
        return JSONResult.ok("编辑企业/VIP成功");
    }

    @Override
    public JSONResult status(UserGroupReq vipReq, RedisUser redisUser) {
        UserGroupDO userGroupExists = vipMapper.uuid(vipReq.getUserGroupUuid());
        if (null == userGroupExists) {
            return JSONResult.errorMap("待查询的企业/VIP不存在");
        }
        UserGroupDO userGroupDO = new UserGroupDO();
        BeanUtils.copyProperties(vipReq, userGroupDO);
        String statusMsg = "";
        //状态 0启用  1禁用
        if ("0".equals(vipReq.getUserGroupStatus())) {
            userGroupDO.setUserGroupStatus("1");
            statusMsg = vipReq.getUserGroupName() + "状态 禁用 成功";
        } else if ("1".equals(vipReq.getUserGroupStatus())) {
            userGroupDO.setUserGroupStatus("0");
            statusMsg = vipReq.getUserGroupName() + "状态 启用 成功";
        }
        userGroupDO.setId(userGroupExists.getId());
        userGroupDO.setUpdateBy(redisUser.getSysUserName());
        userGroupDO.setUpdateTime(DateUtil.getDateTime());
        vipMapper.updateByPrimaryKeySelective(userGroupDO);
        return JSONResult.ok(statusMsg);
    }

    @Override
    public JSONResult rolePage(GroupRoleReq groupRoleReq) {
        UserGroupDO userGroupExists = vipMapper.uuid(groupRoleReq.getUserGroupUuid());
        if (null == userGroupExists) {
            return JSONResult.errorMap("待查询的企业/VIP不存在");
        }
        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(groupRoleReq, roleDO);
        roleDO.setGroupId(userGroupExists.getId());
        PageHelper.startPage(groupRoleReq.getPageNum(), groupRoleReq.getPageSize(), "create_time desc");
        List<RoleDO> roleDOList = roleMapper.rolePage(roleDO);
        PageInfo<RoleDO> pageInfo = new PageInfo<>(roleDOList);
        return JSONResult.ok(pageInfo);
    }

    @Override
    public JSONResult groupRoleAdd(GroupRoleReq groupRoleReq, RedisUser redisUser) {
        UserGroupDO userGroupExists = vipMapper.uuid(groupRoleReq.getUserGroupUuid());
        if (null == userGroupExists) {
            return JSONResult.errorMap("待查询的企业/VIP不存在");
        }
        int roleNameCount = roleMapper.checkRoleRepetition(groupRoleReq.getSysRoleName(), userGroupExists.getId(), null);
        if (roleNameCount > 0) {
            return JSONResult.errorMap("角色名称已存在");
        }
        if (!redisUser.getGroupId().equals(1L)) {
            int checkAdminVipOne = roleMapper.checkAdminVipOne(userGroupExists.getId(), null, groupRoleReq.getSysRoleType());
            if (checkAdminVipOne > 0) {
                return JSONResult.errorMap("管理员角色类型已存在");
            }
        }
        RoleDO userGroupDO = new RoleDO();
        BeanUtils.copyProperties(groupRoleReq, userGroupDO);
        userGroupDO.setGroupId(userGroupExists.getId());
        userGroupDO.setSysRoleUuid(StrUtil.genUUID());
        userGroupDO.setCreateBy(redisUser.getSysUserName());
        userGroupDO.setUpdateBy(redisUser.getSysUserName());
        userGroupDO.setCreateTime(DateUtil.getDateTime());
        userGroupDO.setUpdateTime(DateUtil.getDateTime());
        roleMapper.insertSelective(userGroupDO);
        return JSONResult.ok("新增角色成功");
    }

    private boolean checkInfo(String sysUserEmail, String sysUserPhone, String sysUserLoginName, Long id) {
        return userMapper.checkInfo(sysUserEmail, sysUserPhone, sysUserLoginName, id) > 0 ? true : false;
    }
}
